<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class ModeController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('Mode');
    }
    /**
     * 
    * 模式列表
    * @author wang
    * @param 
    */
    public function modeList()
    {
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $search = I('search');
        if(!empty($search)) $where['m.mode|m.description|g.game'] = array('like','%'.$search.'%');

        $result = M('mode as m')
                ->join('LEFT JOIN '.C('DB_PREFIX').'game as g ON m.gameid = g.gameid')
                ->field('m.*,g.game')
                ->where($where)
                ->order($sort." ".$order)
                ->select();    
        foreach ($result as $k => &$v) {
            $v['gameid']  = $v['gameid'] ?  $v['gameid'] : '';
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
    * 增加模式
    * @author wang
    * @param 
    */
    public function addMode(){
    	$modeid = I('modeid');
        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();
    	if($modeid){
    		if($this->model->where("modeid='$modeid'")->save($data)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '用户修改成功','id'=>$modeid));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '用户修改失败'));
	        }
    	}else{
            // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            $data['createtime']  = time();
    		if($this->model->add($data)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '用户新增成功'));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '用户增加失败'));
	        }
    	}
    }

    /**
    * 删除模式
    * @author wang
    * @param 
    */
    public function delMode(){
        $modeid = I('modeid');

            if($this->model->where("modeid='$modeid'")->delete()){
                M('basescore')->where("modeid='$modeid'")->setField('modeid','');
                $result = array(
                    'sta' => 1, 
                    'msg' => '删除成功'
                );
            }else{
                $result = array(
                    'sta' => 0, 
                    'msg' => '删除失败'
                ); 
            }

        $this->ajaxReturn($result);
    }

    /**
    * 游戏ID
    * @author wang
    * @param 
    */
    public function index(){
        $game_id = M('Game')->select();
        $this->assign("game_id",$game_id);
        $this->display();
    }



}